package com.wink_172.library.adapter

import android.content.Context
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnLongClickListener
import android.view.ViewGroup
import android.widget.ImageView
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.wink_172.library.R
import com.wink_172.library.callback.ICallback
import com.wink_172.library.databinding.ItemView01Binding
import com.wink_172.library.databinding.ItemView02Binding
import com.wink_172.library.model.Constants
import com.wink_172.library.model.BaseDataItem
import com.wink_172.library.model.json.ImageData
import org.xutils.image.ImageOptions
import org.xutils.x

open class HomeListAdapter : RecyclerView.Adapter<HomeListAdapter.BaseViewsHolder>,
    View.OnClickListener, OnLongClickListener {
    open var mDataList: MutableList<Any>? = null
    open var mListview: RecyclerView? = null
    open var mOnItemClickListener: OnItemClickListener? = null  //lateinit全局变量延迟初始化   P202
    open var mOnItemLongClickListener: OnItemLongClickListener? = null
    open var isListSelect = false //选择模式

    open val dataList: MutableList<Any>? get() = mDataList
    open var mContext: Context? = null

    interface OnItemClickListener {
        fun onItemClick(view: View?, position: Int)
    }

    interface OnItemLongClickListener {
        fun onItemLongClick(view: View?, position: Int)
    }

    constructor() {}
    constructor(context: Context, dataList: MutableList<Any>?, listview: RecyclerView) {
        mDataList = dataList
        mContext = context
        mListview = listview
    }

    fun swapContent(dataList: MutableList<Any>?) {
        mDataList = dataList
    }


    //P76  p77 let使用
    fun appendContent(dataList: List<Any>?, before: Boolean) {
        mDataList?.let {
            if (!before) {
                it.addAll(dataList!!)
            } else {
                it.addAll(0, dataList!!)
            }
        }

    }




    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeListAdapter.BaseViewsHolder {
        val viewVolder = createCustomViewHolder(parent, viewType)
        val view = viewVolder!!.itemView
        view.setOnClickListener(this)
        view.setOnLongClickListener(this)
        return viewVolder
    }

    override fun onBindViewHolder(holder: BaseViewsHolder, position: Int) {
        val type = holder!!.itemViewType
        val itemData = getItemDate(position)
        val baseViewsHolder = holder as BaseViewsHolder?
        baseViewsHolder!!.updateContent(itemData)
    }

    override fun getItemViewType(position: Int): Int {
        val itemData = mDataList!![position]
        var sceneType = 0
        if (itemData is BaseDataItem) {
            sceneType = itemData.type
        }
        return sceneType
    }

    override fun getItemCount(): Int {
        return mDataList!!.size
    }

    override fun onClick(view: View) {
        if(mOnItemClickListener==null){
            return
        }
        mOnItemClickListener?.let { mOnItemClickListener ->
            val layoutManger = mListview!!.layoutManager ?: return
            val position = layoutManger.getPosition(view)
            mOnItemClickListener.onItemClick(view, position)

        }
    }

    override fun onLongClick(view: View): Boolean {
        if(mOnItemLongClickListener==null){
            return false
        }
        mOnItemLongClickListener?.let {
            val layoutManager = mListview!!.layoutManager as LinearLayoutManager?
            val position = layoutManager!!.getPosition(view)
            it.onItemLongClick(view, position)
        }
        return false
    }

    private fun getItemDate(position: Int): Any {
        return mDataList!![position]
    }

    /**
     * */
       open class BaseViewsHolder(view: View?) : RecyclerView.ViewHolder(view!!), View.OnClickListener {
        open var mCallback: ICallback? = null//callback_m逐步替换她
        open var callback_m: com.wink_172.library.callback.ICallback? = null

        open fun setCallback(callback: ICallback) {
            mCallback = callback
        }

        interface ICallback {
            fun postObject(event: Int, vararg args: Any?)
        }

        open fun updateContent(itemData: Any?) {}
        override fun onClick(v: View) {}
    }

    open fun createCustomViewHolder(parent: ViewGroup, viewType: Int): HomeListAdapter.BaseViewsHolder? {
        var view: View? = null
        return when (viewType) {
            Constants.List_Enum.间距.ordinal -> {
                view = LayoutInflater.from(parent.context).inflate(R.layout.item_view02, parent, false)

                SpaceViews01Holder(view)
            }
            Constants.List_Enum.选择图片列表.ordinal-> {
                view = LayoutInflater.from(parent.context).inflate(R.layout.item_view01, parent, false)
                GridImageListViewsHolder(view)
            }
            else -> null
        }
    }

    private inner class SpaceViews01Holder(itemView: View?) : BaseViewsHolder(itemView) {
        private val itemView02Binding: ItemView02Binding?
        override fun updateContent(itemData: Any?) {

//        动态设置文字颜色
//        setTextColor(getResources().getColorStateList(R.drawable.selector_text_color));

//            int[] colors = getResources().getIntArray(R.array.colors01);
//            binding.btnSendSms.setTextColor(colors[1]);
        }

        init {
            itemView02Binding = DataBindingUtil.bind(itemView!!)
        }
    }

    private inner class GridImageListViewsHolder(itemView: View?) : BaseViewsHolder(itemView) {
        private val binding: ItemView01Binding?
        private var item: BaseDataItem? = null
        init {
            binding = DataBindingUtil.bind(itemView!!)
            binding!!.btnClose.setOnClickListener(this)
            binding!!.iconLeft.setOnClickListener(this)
        }

        override fun onClick(v: View) {
            super.onClick(v)
            if (v.id == R.id.icon_left) {
                mCallback!!.postObject(Constants.EVENT_01, item)
            } else if (v.id == R.id.btn_close) {
                mCallback!!.postObject(Constants.EVENT_02, item)
            }
        }

        override fun updateContent(itemData: Any?) {
            item = itemData as BaseDataItem?
            mCallback = item!!.callback
            val imageData = item!!.data as ImageData


       //看产品需求了 有些需要边框有些不需要 修改即可 shap_gary_stroke_4dp
            binding!!.btnClose.visibility = View.GONE
            binding.iconLeft.setImageResource(R.mipmap.icon_pz_add)
            binding.iconLeft.scaleType = ImageView.ScaleType.CENTER
            if (!TextUtils.isEmpty(imageData.web_url)) {
                Log.e(TAG, "updateContent  web_url: ====>>"+ imageData.web_url)
                x.image().bind(binding.iconLeft, imageData.web_url)
//                binding.iconLeft.scaleType = ImageView.ScaleType.FIT_XY
                binding.btnClose.visibility = View.VISIBLE
            }else if (!TextUtils.isEmpty(imageData.local_url)) {
                Log.e(TAG, "updateContent  local_url: ====>>"+ imageData.local_url)
                x.image().bind(binding.iconLeft, imageData.local_url)
//                binding.iconLeft.scaleType = ImageView.ScaleType.FIT_XY
                binding.btnClose.visibility = View.VISIBLE

            }
        }


    }

    private  val TAG = "HomeListAdapter"
    companion object {

    }


}